<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="locale/en-US/lng.js"></script>
<script type="text/javascript" src="locale/ru-RU/lng.js"></script>
<script type="text/javascript">
// communication
function sendCommand(msg){
  chrome.windows.getCurrent(function(wnd){
    chrome.tabs.getSelected(wnd.id, function(tab){
      chrome.tabs.connect(tab.id).postMessage({command: msg});
	  window.close();
    });
  });
};

// disable adblock for the current site
var whiteList = {
	prefix: 'userfilters',
	_set: function(a){window.localStorage[this.prefix] = a.join('\n')},
	_get: function(){return (window.localStorage[this.prefix] || '').split('\n')},
	add: function(domain){var a = this._get(); a.unshift('@@||' + domain); this._set(a)},
	del: function(domain){var a = this._get(); for(var i = 0; i < a.length; i++){if(a[i].indexOf('@@||') == 0 && a[i].slice(4) == domain)a.splice(i, 1)}; this._set(a)},
	into: function(domain){var a = this._get(); for(var i = 0; i < a.length; i++){if(a[i].indexOf('@@||') == 0 && a[i].slice(4) == domain)return true}}
};

function getDomain(url){
	var link = document.createElement('a');
	link.href = url;
	return link.host;
};

function disableSubscription(){
	if(window.localStorage['adblock+'])chrome.windows.getCurrent(function(wnd){
		chrome.tabs.getSelected(wnd.id, function(tab){
			var domain = getDomain(tab.url);
			var s = document.getElementById('subscription');
			if(whiteList.into(domain)){
				whiteList.del(domain);
				s.innerHTML = lng.disableSub;
			}
			else{
				whiteList.add(domain);
				s.innerHTML = lng.enableSub;
			};
			var port = chrome.extension.connect({name: 'adblock'});
			port.postMessage({command: 'UpdateFilters'});
			port.postMessage({command: 'UpdateCss'});
		});
	});
};

function createMenu(){
	var menu = '<div class="menu">';
	menu += '<div onclick="sendCommand(\'Block\');">'+lng.block+' <span>Alt+B</span></div>';
	menu += '<div onclick="sendCommand(\'BlockElements\');">'+lng.blockElements+' <span>Alt+W</span></div>';
	menu += '<div onclick="sendCommand(\'Unblock\');">'+lng.unblock+' <span>Alt+U</span></div>';
	menu += '<div onclick="sendCommand(\'UnblockLatest\');">'+lng.unblockLatest+' <span>Alt+L</span></div>';
	menu += '<div onclick="sendCommand(\'Edit\');">'+lng.edit+' <span>Alt+E</span></div>';
	menu += '<hr>';
	menu += '<div id="subscription" onclick="disableSubscription();">'+lng.disableSub+'</div>';
	menu += '<hr>';
	menu += '<div onclick="chrome.tabs.create({url: \'preferences.html\'})">'+lng.preferences+'</div>';
	menu += '</div>';
	document.body.innerHTML = menu;
	chrome.windows.getCurrent(function(wnd){
		chrome.tabs.getSelected(wnd.id, function(tab){
			var ele = document.getElementById('subscription'), notSubscribed = !window.localStorage['adblock+'];
			if(notSubscribed){
				ele.style.opacity = 0.5;
				ele.innerHTML = lng.enableSub;
			}
			else if(whiteList.into(getDomain(tab.url))){
				ele.innerHTML = lng.enableSub;
			}
		});
	});
};
</script>
<style type="text/css">
	body{ overflow: hidden; margin: 0; padding: 4px; font-family: tahoma, sans-serif; width: 240px; white-space: nowrap; }
	.menu div{ background-color: white; color: black; font-size: 11px; padding: 4px 14px; }
	.menu div:hover{ background-color: #316AC5; color: white; cursor: default; }
	.menu div span{ float: right; }
</style>
</head>
<body onload="createMenu()">
</body>
</html>
